using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Util;
using Microsoft.AspNetCore.Http;
using Publicuse.Entity.AttributeUtil;
using System.Linq;
using Publicuse.Application;
using Publicuse.Cache;

namespace Publicuse.Appication
{
    /// <summary>
    /// 数据表代码生成模板信息管理信息管理
    /// </summary>
    public class TbcodegenerationApplication
    {
        private static BaesApplication baseApplication;
        private static TbcodegenerationApplication userApplication;

        public static TbcodegenerationApplication GetApplication(ServiceContainer container)
        {
            baseApplication = new BaesApplication(container);
            return userApplication;
        }

        static TbcodegenerationApplication()
        {
            userApplication = new TbcodegenerationApplication();
        }

        /// <summary>
        /// 获取数据表代码生成模板信息管理模块信息列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<CommonResult> GetList(Tbcodegeneration query)
        {
            return await baseApplication.GetListByExpression<Tbcodegeneration>(query, query.pageIndex, query.pageSize, o => o.Id, false);
        }

        /// <summary>
        /// 批量审核数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<CommonResult> ConfirmAll(string data, string loginUserCode)
        {
            return await baseApplication.ConfirmAll<Tbcodegeneration>(BusinessType.tbcodegeneration, BusinessActionType.check,
              BusinessLogType.writeLogToDataBase, data, loginUserCode);
        }

        /// <summary>
        /// 审核数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<CommonResult> Confirm(string id, int type, string loginUserCode)
        {
            return await baseApplication.Confirm<Tbcodegeneration>(BusinessType.tbcodegeneration, BusinessActionType.check,
              BusinessLogType.writeLogToDataBase, id, type, loginUserCode);
        }
		
		
		/// <summary>
		/// 检查数据表代码生成模板信息管理模块表名称是否存在
		/// </summary>
		/// <param name="name"></param>
		/// <param name="loginUserCode"></param>
		/// <param name="Id"></param>
		/// <returns></returns>
		public async Task<CommonResult> CheckName(string name, string loginUserCode, string Id = "")
		{
			return await baseApplication.CheckDataExists<Tbcodegeneration>(BusinessType.tbcodegeneration,
			BusinessLogType.writeLogToDataBase, o=>o.name==name && o.status!=-9, "检查数据表代码生成模板信息管理模块表名称是否存在", Id);
		}

		/// <summary>
		/// 检查数据表代码生成模板信息管理模块表编码是否存在
		/// </summary>
		/// <param name="code"></param>
		/// <param name="loginUserCode"></param>
		/// <param name="Id"></param>
		/// <returns></returns>
		public async Task<CommonResult> CheckCode(string code, string loginUserCode, string Id = "")
		{
			return await baseApplication.CheckDataExists<Tbcodegeneration>(BusinessType.tbcodegeneration,
			BusinessLogType.writeLogToDataBase, o=>o.code==code&&o.status!=-9, "检查数据表代码生成模板信息管理模块表编码是否存在", Id);
		}


        /// <summary>
        /// 数据表代码生成模板信息管理信息导入
        /// </summary>
        /// <returns></returns>
        public async Task<CommonResult> ImportExcel(IFormFileCollection files, string path, string loginUserCode)
        {
            return await Task.Factory.StartNew(() =>
            {
                CommonResult commonResult = new CommonResult();
                StringBuilder result = new StringBuilder();
                baseApplication.autofaceInstance.DoWork(BusinessType.tbcodegeneration, BusinessActionType.import, files[0].FileName, "数据表代码生成模板信息管理数据", BusinessLogType.writeLogToDataBase, (o) =>
                {
                    commonResult = AddExcelToData(files, path);
                    return commonResult;
                }, null, ref commonResult);
                return commonResult;
            });
        }

        /// <summary>
        /// 从Excel中导入数据到数据库表
        /// </summary>
        private static CommonResult AddExcelToData(IFormFileCollection files,string path)
        {
            StringBuilder result = new StringBuilder();
            CommonResult commonResult = new CommonResult();
            string msg = string.Empty;
            ImportCache.InitImportData(files, path, "数据表代码生成模板信息管理", out msg, (list) => {
                var module = "数据表代码生成模板信息管理";
                Dictionary<int, List<int>> aclist = new Dictionary<int, List<int>>();
                foreach (var dt in list)
                {
                    
                    
                }
                commonResult.data = "导入完成";
                return true;
            });
            if (!string.IsNullOrEmpty(msg))
            {
                commonResult.msg = "请选择数据表代码生成模板信息管理导入";
                commonResult.data = null;
                commonResult.count = 0;
            }
            return commonResult;
        }

        /// <summary>
        /// 新增修改数据表代码生成模板信息管理模块信息
        /// </summary>
        /// <param name="Contacts"></param>
        /// <returns></returns>
        public async Task<CommonResult> AddOrUpdate(Tbcodegeneration info)
        {
            return await baseApplication.AddOrUpdate<Tbcodegeneration>(info, BusinessType.tbcodegeneration, BusinessLogType.writeLogToDataBase);
        }

        /// <summary>
        /// 删除数据表代码生成模板信息管理模块信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<CommonResult> Delete(string Id, string loginUserCode, string type)
        {
            return await baseApplication.Delete<Tbcodegeneration>(Id, loginUserCode, type, BusinessType.tbcodegeneration, (a, b, info) =>
            {
                return info.Id.ToString();
            });
        }

        /// <summary>
        /// 还原数据表代码生成模板信息管理模块信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        public async Task<CommonResult> Reduction(string Id, string loginUserCode)
        {
            return await baseApplication.Reduction<Tbcodegeneration>(Id, loginUserCode, BusinessType.tbcodegeneration, (a, b, info) =>
            {
                return info.Id.ToString();
            });
        }
    }
}
